package com.stanley.alogrithm.backtracking;

import java.util.ArrayList;

public class EnumKCombination {
    public static void main(String[] args) {
        int n = 4, k = 2;
        EnumKCombination ekc = new EnumKCombination();
        ArrayList<ArrayList<Integer>> result = ekc.combine(4, 2);
        System.out.print("result:" + result);
    }

    /**
     *
     * @param n int整型
     * @param k int整型
     * @return int整型ArrayList<ArrayList<>>
     */
    public ArrayList<ArrayList<Integer>> combine (int n, int k) {
        // write code here
        ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>();
        ArrayList<Integer> stack = new ArrayList<Integer>();
        backTracking(n, 1, k, result, stack);
        return result;
    }

    private void backTracking(int n, int start, int k, ArrayList<ArrayList<Integer>> result, ArrayList<Integer> stack) {
        if (k<0) {
            return;
        }
        if(k == 0) {
            result.add(new ArrayList<Integer>(stack));
            return;
        }
        for (int i=start; i<= n; i++) {
            stack.add(i);
            backTracking(n, i+1, k-1, result, stack);
            stack.remove(stack.get(stack.size() - 1));
        }
    }
}
